{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "bbd8820d-3293-4978-88e4-6481c918907d",
   "metadata": {},
   "outputs": [
    {
     "ename": "_IncompleteInputError",
     "evalue": "incomplete input (2511301673.py, line 21)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  Cell \u001b[1;32mIn[2], line 21\u001b[1;36m\u001b[0m\n\u001b[1;33m    print('套索回归使用的特征数为：',a\u001b[0m\n\u001b[1;37m                          ^\u001b[0m\n\u001b[1;31m_IncompleteInputError\u001b[0m\u001b[1;31m:\u001b[0m incomplete input\n"
     ]
    }
   ],
   "source": [
    "#导入套索回归模型、糖尿病数据集及划分样本的方法\n",
    "from sklearn.linear_model import Lasso\n",
    "from sklearn.datasets import load_diabetes\n",
    "from sklearn.model_selection import train_test_split\n",
    "import numpy as np\n",
    "#将数据集划分为训练集和预测集\n",
    "x,y=load_diabetes().data,load_diabetes().target\n",
    "x_train,x_test,y_train,y_test=train_test_split(x,y,random_state=8)\n",
    "#训练模型\n",
    "model=Lasso()\n",
    "\n",
    "model=Lasso(alpha=0.1,max_iter=100000)   #2-9\n",
    "\n",
    "model.fit(x_train,y_train)\n",
    "a=np.sum(model.coef_!=0)   #模型特征属性不等于0的个数\n",
    "#计算模型的预测准确率\n",
    "r21=model.score(x_train,y_train)\n",
    "r22=model.score(x_test,y_test)\n",
    "print('模型在训练集上的预测准确率为:',r21)\n",
    "print('模型在测试集上的预测准确率为:',r22)\n",
    "print('套索回归使用的特征数为：',a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e003a994-2a2c-4e45-81fa-aa95a1de6a27",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
